<?php
class Category extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this -> output -> enable_profiler(PROFILER);
        is_logged_in();
        //$this->load->model("categorymodel");        //Not required if you autoload the library        //$this->load->library('doctrine');        //Instantiate a Doctrine Entity Manager        $this -> em = $this -> doctrine -> em;
    }    public function index() {
        $this -> load -> library('table');
        if ($this -> input -> post('search')) {
            if (array_key_exists('search', $_POST)) {
                $term = $this -> input -> post('search');
                $foundCategories = $this -> em -> getRepository('models\Category') -> findBy(array('name' => $term));            }
            if (count($foundCategories) > 0) {
                $data['categories'] = $foundCategories;
            }
        } else {           if (PAGINATION_ENABLED) {                          $totalOfRows = count($this -> em -> getRepository('models\Category') -> findAll());                        $this -> load -> library('pagination');            $config['base_url'] = site_url() . '/category/index';            $config['total_rows'] = $totalOfRows;
            $this -> pagination -> initialize($config);
            if ($this -> uri -> segment(3)) {
                $offset = $this -> uri -> segment(3);
            } else {
                $offset = 0;
            }            
                $categories = $this -> em -> getRepository('models\Category') -> findBy(array(), array(), $this -> pagination ->per_page, $offset);            }                                          else {                $categories = $this -> em -> getRepository('models\Category') -> findAll();            }
            if ($categories) {                $data['categories'] = $categories;            }
        }
        $data['main_content'] = "admin/category/index";
        $this -> load -> view("admin/template", $data);
    }    public function create() {
        if ($this -> form_validation -> run('category/create') == TRUE) {
            //$id = $this -> categorymodel -> add($_POST);            try {				$category = new models\Category();                models\Category::prepareData($_POST,$category);                $this -> em -> persist($category);                $this -> em -> flush();                $this -> session -> set_flashdata('message', array('title' => 'Category Created', 'content' => 'The category has been sucessfully created', 'type' => ALERT_TYPE_SUCCESS));                redirect('category/index');            } catch(Exception $e) {                $this -> session -> set_flashdata('message', array('title' => 'Category Not Created', 'content' => 'A database Error has occur, please contact your administrator', 'type' => ALERT_TYPE_ERROR));
                redirect('category/index');
            }
        } else {            $data['category'] = new models\Category();
            $data['main_content'] = "admin/category/form";
            $this -> load -> view("admin/template", $data);
        }
    }    public function edit($id) {
        $id = (int)$id;
        $data['id'] = $id;
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            if ($this -> form_validation -> run("category/edit") == TRUE) {
                try {                    $category = $this -> doctrine -> em -> find('models\Category', $id);					models\Category::prepareData($_POST,$category);                    $this -> em -> persist($category);                    $this -> em -> flush();                    $this -> session -> set_flashdata('message', array('title' => 'Category Updated', 'content' => 'The category has been sucessfully updated', 'type' => ALERT_TYPE_SUCCESS));
                    redirect('category/index');
                } catch (Exception $e) {
                    $this -> session -> set_flashdata('message', array('title' => 'Category Not Updated', 'content' => 'A database Error has occur, please contact your administrator', 'type' => ALERT_TYPE_ERROR));
                    redirect('category/index');
                }
            } else {                $data['category'] = new models\Category();
                $data['main_content'] = "admin/category/form";
                $this -> load -> view("admin/template", $data);
            }
        } else {            $data['category'] = $this -> doctrine -> em -> find('models\Category', $id);            // var_dump($data['category'] );die();
            if ($data['category']) {
                $data['main_content'] = "admin/category/form";
                $this -> load -> view("admin/template", $data);
            } else {
                $this -> session -> set_flashdata('message', array('title' => 'Not able to retrieve category registry', 'content' => 'A database Error has occur, please contact your administrator', 'type' => ALERT_TYPE_WARNING));
                redirect('category/index');
            }
        }
    }    public function delete($id) {        try {            $category = $this -> doctrine -> em -> find('models\Category', $id);
            $this -> em -> remove($category);            $this -> em -> flush();
            $this -> session -> set_flashdata('message', array('title' => 'Category deleted', 'content' => 'The category has been sucessfully deleted', 'type' => ALERT_TYPE_SUCCESS));        } catch(Exception $e) {            $this -> session -> set_flashdata('message', array('title' => 'Not able to retrieve category registry', 'content' => 'A database Error has occur, please contact your administrator', 'type' => ALERT_TYPE_WARNING));        }
        redirect('category/index');
    }}?>